#!/bin/bash
# 此脚本用来编译安装apbs-3.4.1软件
# apbs-3.4.1软件介绍
#  
#  Usage:
#     bash /path/to/build.sh 
# 使用build.sh 脚本样例 bash /path/to/build.sh 
# 使用build.sh 需要root权限或者sudo权限,否则无法安装依赖
#
##############################################################
# 导入公共方法脚本
source "$(
  cd "$(dirname "$0")" || exit 1
  pwd
)/common.sh"

function install_development() {
  sudo yum install -y time patch environment-modules systemd-devel zlib-devel zlib bc gcc-c++
  source /etc/profile
  module purge
}

# 创建环境变量脚本
function write_apbs_run() {
  cat >"$1/apbs-hpc/run.sh" <<EOF
#!/bin/bash
sudo yum install -y m4 environment-modules time wget tar git zlib zlib-devel autoconf automake libtool gcc-c++

source /etc/profile &> /dev/null
current_dir="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd -P)"
for f in "\${current_dir}"/*; do
  if [[ -d "\${f}" && -f "\$(ls "\${f}"/*modulefiles 2>&1)" ]]; then
    module use "\${f}" && module load "\${f}"/*modulefiles
  fi
done
cd \$current_dir
if [[ ! -d "$installpath/hmpi-1.1.1" ]]; then
    mkdir -p $installpath/hmpi-1.1.1
    cp -r \$current_dir/hmpi-1.1.1/* $installpath/hmpi-1.1.1/
fi
echo -e "\033[1;32;1mapbs environment initialization completed.\033[0m"
EOF

  chmod +x "$1"/apbs-hpc/run.sh
}

# 安装依赖软件
<<install_dependency_software
  description: 安装依赖软件
  Parameters:
    param1 - 脚本名称
    param2 - 依赖名称
    param3 - modulefiles名称
install_dependency_software
function install_dependency_software() {
  bash "$dep_dir"/"$1".sh "$buildpath" "$installpath"
  module use "$installpath"/"$2"
  module load "$installpath"/"$2"/"$3"_modulefiles
}

# 安装毕晟编译器
function install_bisheng() {
  install_dependency_software "bisheng-compiler-2.1.0" "bisheng-compiler-2.1.0" "bisheng"
}

# 安装hmpi-1.1.1
function install_hmpi() {
  install_dependency_software "hmpi-1.1.1" "hmpi-1.1.1" "hmpi"
}

#安装 apbs
function install_apbs() {
  bash "$4"/apbs-3.4.1.sh "$1" "$2" "$4"
  mkdir -p "$3"/apbs-hpc && cp -r "$2"/* "$3"/apbs-hpc/
}

# 将生成的hpc文件打包到pkg文件夹下
function tar_pkg() {
  echo "# 正在打包 apbs 文件 路径为$1"
  cd "$1" && tar -zcf apbs-3.4.1-hpc.tar.gz apbs-hpc
  echo -e "\033[1;32;1m# apbs 已经打包成功 文件路径为$1/apbs-3.4.1-hpc.tar.gz\033[0m"
}

function main() {

 # 校验执行脚本时的入参
  check_build_param "$1"
  print_current_running_script_info "build.sh"

  # 构建路径
  buildpath=$(create_dir "build")

  # 安装路径
  installpath=$(create_dir "install")

  # 依赖路径
  dep_dir=$(get_current_dir)/dependency

  # 打包路径
  pkg_dir=$(get_current_dir)/pkg

  install_development

  # 安装毕昇编译器
  install_bisheng
  if [ $? -ne 0 ]; then
    echo -e "\033[31m# 正在退出\033[0m"
    exit 1
  fi

  # 安装hmpi
 install_hmpi
  if [ $? -ne 0 ]; then
    echo -e "\033[31m# 正在退出\033[0m"
    exit 1
  fi

  install_apbs "$buildpath" "$installpath" "$pkg_dir" "$(get_current_dir)"
  if [ $? -ne 0 ]; then
    echo -e "\033[31m# 正在退出\033[0m"
    exit 1
  fi


  write_apbs_run "$pkg_dir"

  tar_pkg "$pkg_dir"
  echo " # ------------------------------------------------------------------------"
  echo "# 运行软件前请执行以下命令"
  echo -e "\033[1;32;1m# source ""${pkg_dir}""/apbs-hpc/run.sh\033[0m"
  echo "# 来加载 apbs 运行时环境变量"
  echo "# ------------------------------------------------------------------------"
  rm -rf "$buildpath" "$installpath"
}

main "$@"
